package com.cbgolf.oa.model;

import com.cbgolf.oa.base.BaseModel;
import com.cbgolf.oa.entity.NetResponse;
import com.cbgolf.oa.entity.PointBean;
import com.cbgolf.oa.entity.ShopBean;
import com.cbgolf.oa.inteface.IAsynTask;
import com.cbgolf.oa.net.NetCallBack;
import com.cbgolf.oa.net.NetUtils;
import com.cbgolf.oa.net.Web;
import com.cbgolf.oa.net.WebAPI;
import com.cbgolf.oa.presenter.IShopPresenter;
import com.cbgolf.oa.util.DataUtil;
import com.cbgolf.oa.util.TextUtil;
import com.cbgolf.oa.util.Util;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * @author lq Created by lq on 2017/11/1.
 */

public class ShoppingModel extends BaseModel {


  private IShopPresenter worker;

  public ShoppingModel(IShopPresenter worker) {
    this.worker = worker;
    getPointList();
  }

  public void getPointList() {
    String url = WebAPI.outletList_get;
    worker.showProgress();
    Web.getOK(url, new NetCallBack(response -> {
      worker.stopProgress();
      getPointResult(response);
    }));
  }

  private void getPointResult(NetResponse response) {
    if (NetUtils.SUCCESS != response.statusCode) {
      worker.getPointFail();
      return;
    }
    worker.getPointSuccess(DataUtil.getList(response.result, PointBean.class));
  }

  public void getData(String code) {
    String url = WebAPI.shop_list_get + "/" + code;
    worker.showProgress();
    Web.getOK(url, new NetCallBack(response -> {
      worker.stopProgress();
      List<ShopBean> list = getList(response, ShopBean.class);
//            try {
//                list = JSON.parseArray(response.result, ShopBean.class);
//            } catch (Exception e) {
//                worker.getDataFail();
//                return;
//            }
      if (!Util.listIsNull(list)) {
        filterData(list);
      } else {
        worker.getDataFail(errorMsg, errorCode);
      }

    }));
  }

  private void filterData(List<ShopBean> list) {
    List<ShopBean> listdata = new ArrayList<>();
    Util.asynTask(new IAsynTask() {
      @Override
      public void updateUI(Serializable ser) {
        worker.getDataSuccess(listdata);
      }

      @Override
      public Serializable Run() {
        for (int i = 0; i < list.size(); i++) {
          if (!Util.listIsNull(list.get(i).goods)) {
            listdata.add(list.get(i));
          }
        }
        return null;
      }
    });


  }

  public void selectCardNum(String num) {
    String url = WebAPI.shop_cardnum_select_get + "/" + num;
    worker.showProgress();
    Web.getOK(url, new NetCallBack(response -> {
      worker.stopProgress();
      if (NetUtils.SUCCESS != response.statusCode) {
        worker.selectCardNumFail();
        return;
      }
      worker.selectCardNumSuccess(getList(response, ShopBean.class));
    }));

  }

  public void subMit(ShopBean bean) {
    if (null == bean) {
      Util.showNetError();
      return;
    }
    String url = WebAPI.kandai_post;
    Map<String, Object> map = new HashMap<>();
    final JSONObject json = new JSONObject();
    JSONArray array = new JSONArray();
    try {
      if (!Util.listIsNull(bean.goods)) {
        List<ShopBean.goodsDetails> list = bean.goods;
        JSONObject goods;
        for (int i = 0; i < list.size(); i++) {
          goods = new JSONObject();
          if (Util.isNull(list.get(i).goodsId)) {
            Util.show("很抱歉，商品-" + (TextUtil.Text(list.get(i).goodName)) + "-异常");
            return;
          }
          goods.put("goodsId", TextUtil.Text(list.get(i).goodsId));
          goods.put("consumeNumber", TextUtil.Text(list.get(i).num));
          array.put(goods);
        }
      }
      json.put("outletCode", TextUtil.Text(bean.pointCode));
      json.put("consumerCarId", TextUtil.Text(bean.cardId));
      json.put("customerId", TextUtil.Text(bean.userId));
      json.put("goods", array);
    } catch (JSONException e) {
      e.printStackTrace();
    }
    map.put("token", DataUtil.getToken());
    map.put("dto", json);
    worker.showProgress();
    Web.postJsonOk(url, map, new NetCallBack(response -> {
      worker.stopProgress();
      if (NetUtils.SUCCESS != response.statusCode) {
        worker.submitFail(Util.isEquals(response.errorMsg, "may not be empty") ?
            "很抱歉，商品异常" : response.errorMsg);
        return;
      }
      worker.submitSuccess(getJsonString(response, "identifier"));

    }));

  }
}
